## --------------------------
## Panel A
## --------------------------
rm(list = ls())
load("dataBJPOLS.RData")

inst.1 <- "judgeiv_hd"
endo.1 <- "pti"
outc.1 <- "vote_post"

time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow) + as.factor(severity)"
demo.controls <- "age + I(age^2) +  as.factor(race4) + female + vote_pre + as.factor(noteli) + regis_before"
demo.controls.j <- "age + I(age^2) +  as.factor(race4) + female + vote_pre + as.factor(noteli) + regis_before + as.factor(judge_race)"
case.controls <- "as.factor(any_drug) +  as.factor(any_weapon) +  as.factor(any_prop) + as.factor(any_prior_case)"

form.1 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls))
form.2 <- formula(paste(outc.1, "~", time.controls, "+", demo.controls.j, "+", case.controls))
form.3 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls.j, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls.j, "+", case.controls))

resultsB <- resultsH <- resultsW <- resultsM <- resultsHM <- resultsWM <- list()
boot.run <- T

if(boot.run) {
  set.seed(1231)
  for(i in 1:500) {
    index <- sample(1:nrow(last.cases), nrow(last.cases), replace = TRUE)
    m1a1 <- ivreg(form.1, data = last.cases[index, ])
    m1a2 <- ivreg(form.2, data = last.cases[index, ])
    m1a3 <- ivreg(form.3, data = last.cases[index, ])
    
    resultsB[[i]] <- c(m1a1$coefficients["pti"], 
                       NA, 
                       m1a3$coefficients["pti"])
    
    resultsW[[i]] <- c(NA, 
                       m1a2$coefficients["as.factor(judge_race)W"], 
                       m1a3$coefficients["as.factor(judge_race)W"])
    
    resultsH[[i]] <- c(NA, 
                       m1a2$coefficients["as.factor(judge_race)H"], 
                       m1a3$coefficients["as.factor(judge_race)H"])
  }
  
  SEsB <- round(apply(do.call('rbind', resultsB), 2, sd, na.rm = T), 3)
  SEsW <- round(apply(do.call('rbind', resultsW), 2, sd, na.rm = T), 3)
  SEsH <- round(apply(do.call('rbind', resultsH), 2, sd, na.rm = T), 3)
}

m1a1 <- ivreg(form.1, data = last.cases)
m1a2 <- ivreg(form.2, data = last.cases)
m1a3 <- ivreg(form.3, data = last.cases)

m1a1_d <- summary(m1a1, vcov = sandwich, diagnostic = T)
m1a3_d <- summary(m1a3, vcov = sandwich, diagnostic = T)

res1_pti <- c(coefficients(m1a1)[grep("pti|as.factor\\(judge_race\\)H|as.factor\\(judge_race\\)W", names(coefficients(m1a1)))], NA, NA)
res2_pti <- c(NA, coefficients(m1a2)[grep("pti|as.factor\\(judge_race\\)H|as.factor\\(judge_race\\)W", names(coefficients(m1a2)))])
res3_pti <- coefficients(m1a3)[grep("pti|as.factor\\(judge_race\\)H|as.factor\\(judge_race\\)W", names(coefficients(m1a3)))]


p1 <- data.table(rbind(res1_pti, res2_pti, res3_pti))
p1$V4 <- 1
p1$V5 <- 1:nrow(p1)
p1$V6 <- "Estimate"
colnames(p1) <- paste0("V", 1:6)

p2 <- data.table(cbind(SEsB, SEsW, SEsH))
p2$V4 <- 2
p2$V5 <- 1:nrow(p2)
p2$V6 <- "Std. Error"
colnames(p2) <- paste0("V", 1:6)

table <- rbind(p1, p2)
table <- table[order(V5, V4), ]
table$V4 <- table$V5 <- NULL
p4 <- data.table(m1a1$nobs, m1a2$nobs, m1a3$nobs)    
p4$V6 <- "N"

table <- rbind(table, p4)
table <- table[, c("V6", "V1", "V2", "V3")]
colnames(table) <- c("Variable", "PTI", "Hispanic Judge", "White Judge")
table$model <- c(rep("Model 1", 2), rep("Model 2", 2), rep("Model 3", 2), NA)
table

## --------------------------
## Panel B
## --------------------------
rm(list = ls())
load("dataBJPOLS.RData")
last.cases <- last.cases[race4 == "B", ]

inst.1 <- "judgeiv_hd"
endo.1 <- "pti"
outc.1 <- "vote_post"

time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow) + as.factor(severity)"
demo.controls <- "age + I(age^2) + female + vote_pre + as.factor(noteli) + regis_before"
demo.controls.j <- "age + I(age^2) + female + vote_pre + as.factor(noteli) + regis_before + as.factor(judge_race)"
case.controls <- "as.factor(any_drug) +  as.factor(any_weapon) +  as.factor(any_prop) + as.factor(any_prior_case)"

form.1 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls))
form.2 <- formula(paste(outc.1, "~", time.controls, "+", demo.controls.j, "+", case.controls))
form.3 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls.j, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls.j, "+", case.controls))

resultsB <- resultsH <- resultsW <- resultsM <- resultsHM <- resultsWM <- list()
boot.run <- T

if(boot.run) {
  set.seed(1231)
  for(i in 1:500) {
    index <- sample(1:nrow(last.cases), nrow(last.cases), replace = TRUE)
    m1a1 <- ivreg(form.1, data = last.cases[index, ])
    m1a2 <- ivreg(form.2, data = last.cases[index, ])
    m1a3 <- ivreg(form.3, data = last.cases[index, ])
    
    resultsB[[i]] <- c(m1a1$coefficients["pti"], 
                       NA, 
                       m1a3$coefficients["pti"])
    
    resultsW[[i]] <- c(NA, 
                       m1a2$coefficients["as.factor(judge_race)W"], 
                       m1a3$coefficients["as.factor(judge_race)W"])
    
    resultsH[[i]] <- c(NA, 
                       m1a2$coefficients["as.factor(judge_race)H"], 
                       m1a3$coefficients["as.factor(judge_race)H"])
  }
  
  SEsB <- round(apply(do.call('rbind', resultsB), 2, sd, na.rm = T), 3)
  SEsW <- round(apply(do.call('rbind', resultsW), 2, sd, na.rm = T), 3)
  SEsH <- round(apply(do.call('rbind', resultsH), 2, sd, na.rm = T), 3)
}

m1a1 <- ivreg(form.1, data = last.cases)
m1a2 <- ivreg(form.2, data = last.cases)
m1a3 <- ivreg(form.3, data = last.cases)

m1a1_d <- summary(m1a1, vcov = sandwich, diagnostic = T)
m1a3_d <- summary(m1a3, vcov = sandwich, diagnostic = T)

res1_pti <- c(coefficients(m1a1)[grep("pti|as.factor\\(judge_race\\)H|as.factor\\(judge_race\\)W", names(coefficients(m1a1)))], NA, NA)
res2_pti <- c(NA, coefficients(m1a2)[grep("pti|as.factor\\(judge_race\\)H|as.factor\\(judge_race\\)W", names(coefficients(m1a2)))])
res3_pti <- coefficients(m1a3)[grep("pti|as.factor\\(judge_race\\)H|as.factor\\(judge_race\\)W", names(coefficients(m1a3)))]

p1 <- data.table(rbind(res1_pti, res2_pti, res3_pti))
p1$V4 <- 1
p1$V5 <- 1:nrow(p1)
p1$V6 <- "Estimate"
colnames(p1) <- paste0("V", 1:6)

p2 <- data.table(cbind(SEsB, SEsW, SEsH))
p2$V4 <- 2
p2$V5 <- 1:nrow(p2)
p2$V6 <- "Std. Error"
colnames(p2) <- paste0("V", 1:6)

table <- rbind(p1, p2)
table <- table[order(V5, V4), ]
table$V4 <- table$V5 <- NULL
p4 <- data.table(m1a1$nobs, m1a2$nobs, m1a3$nobs)    
p4$V6 <- "N"

table <- rbind(table, p4)
table <- table[, c("V6", "V1", "V2", "V3")]
colnames(table) <- c("Variable", "PTI", "Hispanic Judge", "White Judge")
table$model <- c(rep("Model 1", 2), rep("Model 2", 2), rep("Model 3", 2), NA)
table

## --------------------------
## Panel C
## --------------------------
rm(list = ls())
load("dataBJPOLS.RData")
last.cases <- last.cases[race4 == "H", ]

inst.1 <- "judgeiv_hd"
endo.1 <- "pti"
outc.1 <- "vote_post"

time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow) + as.factor(severity)"
demo.controls <- "age + I(age^2) + female + vote_pre + as.factor(noteli) + regis_before"
demo.controls.j <- "age + I(age^2) + female + vote_pre + as.factor(noteli) + regis_before + as.factor(judge_race)"
case.controls <- "as.factor(any_drug) +  as.factor(any_weapon) +  as.factor(any_prop) + as.factor(any_prior_case)"

form.1 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls))
form.2 <- formula(paste(outc.1, "~", time.controls, "+", demo.controls.j, "+", case.controls))
form.3 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls.j, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls.j, "+", case.controls))

resultsB <- resultsH <- resultsW <- resultsM <- resultsHM <- resultsWM <- list()
boot.run <- T

if(boot.run) {
  set.seed(1231)
  for(i in 1:500) {
    index <- sample(1:nrow(last.cases), nrow(last.cases), replace = TRUE)
    m1a1 <- ivreg(form.1, data = last.cases[index, ])
    m1a2 <- ivreg(form.2, data = last.cases[index, ])
    m1a3 <- ivreg(form.3, data = last.cases[index, ])
    
    resultsB[[i]] <- c(m1a1$coefficients["pti"], 
                       NA, 
                       m1a3$coefficients["pti"])
    
    resultsW[[i]] <- c(NA, 
                       m1a2$coefficients["as.factor(judge_race)W"], 
                       m1a3$coefficients["as.factor(judge_race)W"])
    
    resultsH[[i]] <- c(NA, 
                       m1a2$coefficients["as.factor(judge_race)H"], 
                       m1a3$coefficients["as.factor(judge_race)H"])
  }
  
  SEsB <- round(apply(do.call('rbind', resultsB), 2, sd, na.rm = T), 3)
  SEsW <- round(apply(do.call('rbind', resultsW), 2, sd, na.rm = T), 3)
  SEsH <- round(apply(do.call('rbind', resultsH), 2, sd, na.rm = T), 3)
}

m1a1 <- ivreg(form.1, data = last.cases)
m1a2 <- ivreg(form.2, data = last.cases)
m1a3 <- ivreg(form.3, data = last.cases)

m1a1_d <- summary(m1a1, vcov = sandwich, diagnostic = T)
m1a3_d <- summary(m1a3, vcov = sandwich, diagnostic = T)

res1_pti <- c(coefficients(m1a1)[grep("pti|as.factor\\(judge_race\\)H|as.factor\\(judge_race\\)W", names(coefficients(m1a1)))], NA, NA)
res2_pti <- c(NA, coefficients(m1a2)[grep("pti|as.factor\\(judge_race\\)H|as.factor\\(judge_race\\)W", names(coefficients(m1a2)))])
res3_pti <- coefficients(m1a3)[grep("pti|as.factor\\(judge_race\\)H|as.factor\\(judge_race\\)W", names(coefficients(m1a3)))]


p1 <- data.table(rbind(res1_pti, res2_pti, res3_pti))
p1$V4 <- 1
p1$V5 <- 1:nrow(p1)
p1$V6 <- "Estimate"
colnames(p1) <- paste0("V", 1:6)

p2 <- data.table(cbind(SEsB, SEsW, SEsH))
p2$V4 <- 2
p2$V5 <- 1:nrow(p2)
p2$V6 <- "Std. Error"
colnames(p2) <- paste0("V", 1:6)

table <- rbind(p1, p2)
table <- table[order(V5, V4), ]
table$V4 <- table$V5 <- NULL
p4 <- data.table(m1a1$nobs, m1a2$nobs, m1a3$nobs)    
p4$V6 <- "N"

table <- rbind(table, p4)
table <- table[, c("V6", "V1", "V2", "V3")]
colnames(table) <- c("Variable", "PTI", "Hispanic Judge", "White Judge")
table$model <- c(rep("Model 1", 2), rep("Model 2", 2), rep("Model 3", 2), NA)
table
